﻿using System;
using Foothill.Common;
using Foothill.IoC;
using Foothill.Service.AdService;
using Foothill.Service.Session;

namespace Foothill.Web
{
    public partial class EditPostForm : FoothillBasePage
    {
        private ISessionService _sessionService;
      
        protected void Page_Load(object sender, EventArgs e)
        {
            _sessionService = IocContainer.GetInstance<ISessionService>();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            int maskedEditPostId;
            string editkey;

            if (Validate(out maskedEditPostId, out editkey))
            {
                Response.Redirect(BuildEditPostLink(maskedEditPostId, editkey));
            }
        }

        private string BuildEditPostLink(int maskedEditPostId, string editKey)
        {
            return string.Format("~/EditPost/EditPost.aspx?{0}={1}&{2}={3}",
                GlobalQueryString.EditPostIdQsName, maskedEditPostId, GlobalQueryString.EditPostEditKeyQsName, editKey);
        }

        private bool Validate(out int maskedEditPostId, out string editkey)
        {
            string postIdStr = txtPostId.Text.Trim();
            editkey = txtEditKey.Text.Trim();

            if (!SecurityUtility.IsValidPostId(postIdStr, out maskedEditPostId))
            {
                return false;
            }

            if (!SecurityUtility.IsValidEditKey(editkey))
            {
                return false;
            }

            return true;
        }
    }
}
